'use strict';

    angular.module('app')
        .controller('fPasswordController', ['$scope', '$rootScope', '$state', '$timeout', 'toaster',
                                             function($scope, $rootScope, $state, $timeout, toaster) {
        	
        	$scope.record = {};
        	$scope.title="密码重置";
        	validate();
        	// 新增、修改
        	$scope.submit= function(){
        		
                var m = $scope.record;
                if(m.password==null){
                	alert("新密码不能为空！");
                	return false;
                }else if(m.password1==null){
                	alert("确认新密码不能为空！");
                	return false;
                }else if(m.password != m.password1){
                	alert("两次输入密码不一致！");
                	return false;
                }else{
                	if(m){
                        $scope.isDisabled = true;
                        $.ajax({
            				type: 'POST',
            	            dataType: 'json',
            				contentType:'application/json;charset=UTF-8',
            				url : '/user/forgetPassword/updatePassword',
            				data: angular.toJson($scope.record)
            			}).then(function(result){
            				if(result.httpCode ==200){// 成功
	                            toaster.clear('*');
	                            toaster.pop('success', '', "保存成功");
	                            $timeout(function(){
	                                $state.go("access.login");
	                            },2000);
	                        }else{
	                            toaster.clear('*');
	                            toaster.pop('error', '', result.msg);
	                            $scope.isDisabled = false;
	                        }
            			});
                    }
                }
                
            }
        	
        	// 表单校验
        	function validate(){
                jQuery('form').validate({
                	rules: {
                		phone: {
                    		required: true,
							maxLengthB : [ 11 ]
                    	},
                    	smsCode: {
                    		required: true
                    	},
                    	password: {
                    		required: true,
                            maxLengthB : [ 20 ]
                    	},
                    	password1: {
            		        required: true,
                            maxLengthB : [ 20 ]
            	        }
                    },
                    messages: {
                    	phone: {
                    		required: '手机号不能为空',
                            maxLengthB : "请输入正确的手机号码"
                    	},
                    	smsCode: {
                    		required: '请输入正确验证码'
                    	},
                    	password: {
                    		required: '新密码不能为空',
                            maxLengthB : "密码长度不能超过{0}个字符"
                    	},
                    	password1: {
                		    required: '再次输入的密码不一致'
                	    }
                    },
                    submitHandler: function() {
                        $scope.submit();
                    }
                });
            }
        	
        	//手机获取验证码
        	$scope.getSmsCode = function (phone) {
        	    var PhoneReg = /^[1][3,4,5,7,8][0-9]{9}$/;
                if(phone == null || phone == undefined){
                    toaster.clear('*');
                    toaster.pop('error', '', '请填写正确的手机号码');
                }
        		if((phone !== null && PhoneReg.test(phone))){

        			$.ajax({
            			type: 'POST',
                        dataType: 'json',
            			contentType:'application/json;charset=UTF-8',
            			url : '/user/forgetPassword/getSmsCode',
            			data: angular.toJson($scope.record)
            		}).then(function (result) {
                        $scope.loading = false;
                        if (result.httpCode == 200) {
                        	toaster.clear('*');
                            toaster.pop('success', '', "验证码已发送，请注意接收！");
                            var obj = $('#getCode');
                            var countdown=60;
                            settime(obj,countdown,phone);
                        } else {
                        	toaster.clear('*');
                            toaster.pop('error', '', result.msg);
                        }
                        $scope.$apply();
                    });
        		}

        	}


    }]);
    
//验证码倒计时
    function settime(obj,countdown,phone) {
        if (countdown == 0) {
            obj.removeAttr("disabled");
            obj.html("免费获取验证码");
            countdown = 60;
            return;
        } else {
            obj.html("重新发送" + countdown);
            obj.attr("disabled", true);
            countdown--;
        }
        setTimeout(function() {
            settime(obj,countdown)
        },1000)
    }